package com.linkhub.oms.test.validate;

import com.linkhub.oms.admin.modules.order.domain.param.ManualImportOrderParam;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.util.Set;

/**
 * @ClassName validate
 * @Description: com.linkhub.oms.test.validate
 * @Author: Jaycle
 * @CreateDate: 2024/11/25 16:02
 * @Version: 1.0
 */
public class validate {
    public static void main( String[] args ) {
        // 创建 ManualImportOrderParam 对象并设置一些数据
        ManualImportOrderParam param = new ManualImportOrderParam();

        // 设定 orderInfo 和 addressInfo 的值，可能让某些字段不符合校验要求
        ManualImportOrderParam.OrderInfo orderInfo = new ManualImportOrderParam.OrderInfo();
        orderInfo.setOrderNumber("");  // 空的订单号，应该触发验证失败
        param.setOrderInfo(orderInfo);

        ManualImportOrderParam.AddressInfo addressInfo = new ManualImportOrderParam.AddressInfo();
        addressInfo.setCountryCode("AB");
        param.setAddressInfo(addressInfo);

        // 创建验证器
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        Validator validator = factory.getValidator();

        // 进行验证
        Set<ConstraintViolation<ManualImportOrderParam>> violations = validator.validate(param);

        // 如果有验证失败，输出错误信息
        if (!violations.isEmpty()) {
            for (ConstraintViolation<ManualImportOrderParam> violation : violations) {
                System.out.println("Field: " + violation.getPropertyPath() + ", Message: " + violation.getMessage());
            }
        } else {
            System.out.println("Validation passed!");
        }
    }


}
